﻿using System;
using System.Data;
using System.Collections;
using CRS.Core.DataAccess;
using System.Collections.Specialized;
using VitCode.Castle.DataAccess;
using CRS.Core.Entities;

namespace CRS.Web.BaseData
{
    public partial class InventoryList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>");
            
            pgList.DataSourceDelegate=new VitCode.Web.UI.PaginationGrid.GridDataSourceDelegate(BindData);
            if(!IsPostBack)
            {
                Session["InventorySource"] = null;
                GetInventoryInfo("");
                pgList.BindGrid();
            }
        }

        private DataTable DataSource
        {
            get
            {
                if (Session["InventorySource"] == null)
                {
                    Session["InventorySource"] = new DataTable();
                }
                return (DataTable)Session["InventorySource"];
            }
            set { Session["InventorySource"] = value; }
        }

        private object BindData()
        {
            return DataSource;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("InventoryEdit.aspx");
        }

        protected void pgList_OnGridItemSelect(object sender, VitCode.Web.UI.PaginationGridItemSelectArgs e)
        {
            Response.Redirect("InventoryEdit.aspx?billNo="+pgList.SelectedDataKeyValue);
        }

        private void GetInventoryInfo(string condition)
        {
            var access = new CoreDataAccess();       
            var sql =
                @"select i.*,u.Name,c.Name from dbo.CRS_Core_InventoryInfo i 
                    left join dbo.VitCode_Castle_User u on i.CreateUserId=u.Id
                    left join dbo.VitCode_Castle_ContactInfo c on i.ContactId=c.Id where u.Code='"+GenericContext.CurrUserCode+"'";
            sql += condition;
            DataSource=access.GetDataTableBySql(sql);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            var condition = string.Empty;
            if (!string.IsNullOrEmpty(TextBox1.Text.Trim()))
                condition += " and WoCode='" + TextBox1.Text.Trim() + "'";
            if(!string.IsNullOrEmpty(DateCtl1.Text.Trim())&&!string.IsNullOrEmpty(DateCtl2.Text.Trim()))
            {
                    condition += " and CreateDate between '" + Convert.ToDateTime(DateCtl1.Text.Trim()) + "' and '" +
                                 Convert.ToDateTime(DateCtl2.Text.Trim())+"'";
            }
            if (CheckBox1.Checked)
                condition += " and State='新建'";
            if (CheckBox2.Checked)
            {
                condition += " or State='盘点中'";
            }
            if (CheckBox3.Checked)
                condition += " or State='已完成'";
            GetInventoryInfo(condition);
            pgList.BindGrid();
        }
    }
}
